package com.shilan.administrator.qintfm;



import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v4.app.FragmentActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;


import com.shilan.administrator.qintfm.fragment.MainFragment;
import com.shilan.administrator.qintfm.service.PlayService;

public class MainActivity extends BaseActivity implements View.OnClickListener {
    private TabHost tabHost;
    private android.support.v4.app.FragmentManager fragmentManager;
    private LinearLayout linearLayout;
    private ImageView miniBarIv;

    public static PlayService playService;

    public static PlayService.PlayBinder playBinder;
    private boolean isBound = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bindPlayService();
//        //Tabhost这些引用系统ID的findById的时候是使用android.R这个
//        tabHost = (TabHost) findViewById(android.R.id.tabhost);
//        tabHost.setup();//初始化
        miniBarIv= (ImageView) findViewById(R.id.minibar_Iv);
        miniBarIv.setOnClickListener(this);

        linearLayout = (LinearLayout) findViewById(R.id.activity_mian_minibar_Lv);

//
//        init();
      initView();

    }


    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            playBinder = (PlayService.PlayBinder) service;
            playService = playBinder.getPlayService();
            playService.setMusicUpdateListener(musicUpdateListener);
            musicUpdateListener.onChange(playService.getCurrentPosition());
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            playService = null;
            isBound = false;
        }
    };




    //绑定服务
    public void bindPlayService() {
        if (!isBound) {
            Intent intent = new Intent(this, PlayService.class);
            bindService(intent, connection, Context.BIND_AUTO_CREATE);
            startService(intent);
            isBound = true;
        }
    }

    //解除绑定服务
    public void unbindPlayService() {
       playService=null;
        playBinder=null;
        unbindService(connection);
    }


    @Override
    public void publish(int progress) {

    }

    @Override
    public void change(int position) {

    }

//    private void init() {
//
//        TabHost.TabSpec tab1 = tabHost.newTabSpec("我的");
//        View view = getLayoutInflater().inflate(R.layout.tab_mine,null);
//        tab1.setIndicator(view);
//        //绑定占位fragment
//        tab1.setContent(R.id.mineView);
//        tabHost.addTab(tab1);
//
//        TabHost.TabSpec tab2 = tabHost.newTabSpec("发现");
//        View view2 = getLayoutInflater().inflate(R.layout.tab_found,null);
//        tab2.setIndicator(view2);
//        tab2.setContent(R.id.foundView);
//        tabHost.addTab(tab2);
//
//        TabHost.TabSpec tab3 = tabHost.newTabSpec("下载");
//        View view3 = getLayoutInflater().inflate(R.layout.tab_download,null);
//        tab3.setIndicator(view3);
//        tab3.setContent(R.id.downloadView);
//        tabHost.addTab(tab3);
//    }


    private void initView() {

        fragmentManager = getSupportFragmentManager();
        android.support.v4.app.FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.replace(R.id.mainview_framelayout,new MainFragment());
//        ft.replace(R.id.foundView,new FoundFragment());
//        ft.replace(R.id.downloadView, new DownLoadFragment());
        ft.commit();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.minibar_Iv:
                Intent intent = new Intent(this,PlayMusicActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.activity_in,R.anim.activity_out);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        unbindPlayService();
        super.onDestroy();
    }

    // 隐藏底部音乐播放栏
    public void Gone() {
        linearLayout.setVisibility(View.GONE);
        Log.d("MainActivity", "gone");
    }

    // 再次显示底部音乐播放栏
    public void Visible() {
        linearLayout.setVisibility(View.VISIBLE);

    }
}
